package freespiritsocial.io.user.viewmodel

import androidx.lifecycle.MutableLiveData
import com.sendbird.android.SendBirdException
import com.sendbird.android.SendBirdPushHelper
import freespiritsocial.io.user.R
import freespiritsocial.io.user.chat.ConnectionManager
import freespiritsocial.io.user.data.model.BtnItems
import freespiritsocial.io.user.data.model.bean.UserInfo
import freespiritsocial.io.user.network.apiService
import freespiritsocial.io.user.network.upload.IUploadApi
import freespiritsocial.io.user.network.upload.UploadDelegate
import freespiritsocial.io.user.util.CacheUtil
import freespiritsocial.io.user.util.PushUtils
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import java.io.File

class SettingViewModel: BaseViewModel(),IUploadApi by UploadDelegate() {
    val moreList = arrayListOf<BtnItems>(
        BtnItems(appContext.getString(R.string.faqs)),
        BtnItems(appContext.getString(R.string.contact_us)),
        BtnItems(appContext.getString(R.string.community_guidelines)),
        BtnItems(appContext.getString(R.string.terms)),
        BtnItems(appContext.getString(R.string.delete_account)),
    )
    val user = MutableLiveData<UserInfo>(CacheUtil.getUser())

    fun deleteAccount(action: () -> Unit) {
        request({
            apiService.deleteAccount()
        },{
            action()
        })
    }

    fun updateUserInfo(action: () -> Unit) {
        request({
            user.value!!.run {
                val file = File(userAvatar)
                if (file.isFile) {
                    val userAvatar = uploadFile(file)?.url ?: ""
                    this.userAvatar = userAvatar
                }
                apiService.updateUserInfo(this)
            }
        },{
            CacheUtil.refreshUserInfo(user.value!!)
            action()
        },isShowDialog = true)
    }

    fun logout(action: () -> Unit) {
        request({
            apiService.logout()
        },{
            action()
        },isShowDialog = true)
    }

}